import pygame
from pygame.locals import *
import string

from gui_widgets import *
from events import *
from utils import load_png
import preferences

#------------------------------------------------------------------------------
class GUIController:
	"""..."""
	def __init__(self, evManager, game):
		self.evManager = evManager
		self.evManager.RegisterListener( self )
		self.game = game


	#----------------------------------------------------------------------
	def HandlePyGameEvent(self, event):
		"""is given a pygame.event and is responsible for generating
		an event defined in the local events module, or doing nothing"""
		pass

	#----------------------------------------------------------------------
	def Notify(self, event):
		pass


#------------------------------------------------------------------------------
class SimpleGUIController(GUIController):
	"""..."""
	#----------------------------------------------------------------------
	def WantsEvent( self, event ):
		if event.type == KEYDOWN \
		  or event.type == MOUSEBUTTONUP \
		  or event.type == MOUSEMOTION:
			return 1

		return 0

	#----------------------------------------------------------------------
	def HandlePyGameEvent(self, event):
		ev = None

		if event.type == KEYDOWN \
		     and event.key == K_ESCAPE:
			ev = QuitEvent()

		elif event.type == KEYDOWN \
		     and event.key == K_UP:
			ev = GUIFocusPrevWidgetEvent()

		elif event.type == KEYDOWN \
		     and event.key == K_DOWN:
			ev = GUIFocusNextWidgetEvent()

		elif event.type == KEYDOWN \
		     and (event.key == K_RETURN \
		     or event.key == K_SPACE):
			ev = GUIPressEvent()

		elif event.type == KEYDOWN :
			character = str(event.unicode)
			if character and character in string.printable:
				ev = GUIKeyEvent(character)
			elif event.key == K_BACKSPACE:
				ev = GUIControlKeyEvent( event.key )


		elif event.type == MOUSEBUTTONUP:
			b = event.button
			if b == 1:
				ev = GUIClickEvent(event.pos)

		elif event.type == MOUSEMOTION:
			ev = GUIMouseMoveEvent(event.pos)


		if ev:
			self.evManager.Post( ev )


#------------------------------------------------------------------------------
class BlockingDialogController(GUIController):
	"""..."""
	def __init__(self, evManager ):
		self.evManager = evManager
		self.evManager.RegisterListener( self )


	#----------------------------------------------------------------------
	def WantsEvent( self, event ):
		return 1

	#----------------------------------------------------------------------
	def HandlePyGameEvent(self, event):
		ev = None


		if event.type == KEYDOWN \
		     and (event.key == K_RETURN \
		     or event.key == K_SPACE):
			ev = GUIPressEvent()


		elif event.type == MOUSEMOTION:
			ev = GUIMouseMoveEvent(event.pos)

		elif event.type == MOUSEBUTTONUP:
			b = event.button
			if b == 1:
				ev = GUIClickEvent(event.pos)

		if ev:
			self.evManager.Post( ev )


#------------------------------------------------------------------------------
class GUIView(WidgetContainer):
	"""..."""
	def __init__( self, evManager, renderGroup, rect, game):
		WidgetContainer.__init__( self, evManager, rect )

		self.renderGroup = renderGroup

		self.game = game


#------------------------------------------------------------------------------
class BlockingDialogView(GUIView):
	"""..."""
	clipRect = pygame.Rect( (20,20,400,400) )

	#----------------------------------------------------------------------
	def __init__(self, evManager, renderGroup, rect, game, eventConfirm=None, fechar='Fechar'):
		GUIView.__init__( self, evManager, renderGroup, rect, game)

		self.__msg = "Error"
		bgSprite = pygame.sprite.Sprite()
		bgSprite.image = load_png( 'dialog_background.png' )
		#bgSprite.rect = bgSprite.image.get_rect()
		bgSprite.rect = self.rect

		closeEvent = GUIDialogRemoveRequest( 'msgDialog' )
		closeButton = ButtonSprite( evManager, fechar, 
		                            container=self, 
		                            onClickEvent=closeEvent, fontSize=20 )

		self.background = bgSprite
		self.widgets = [ closeButton,
		            LabelSprite(evManager, self.__msg, container=self,
                                        fontColor=(255,255,255)),
		          ]

                if not (eventConfirm == None):
                        confirmButton = ButtonSprite( evManager, 'Confirmar',
                                                container=self,
                                                onClickEvent=eventConfirm, fontSize=20)
                        self.widgets.append(confirmButton)

		#because this is a Dialog, it should be shown on the topmost
		#layer of the View.
		#the renderGroup here is expected to be a LayeredSpriteGroup
		self.renderGroup.add_top( self.background )
		for wid in self.widgets:
			self.renderGroup.add_top( wid )

		self.ArrangeWidgets()

	#----------------------------------------------------------------------
 	def SetMsg(self, msg):
		self.__msg = msg
		self.widgets[1].SetText(self.__msg)

	#----------------------------------------------------------------------
 	def ArrangeWidgets(self ):
		xyOffset = ( self.rect.x, self.rect.y )
		self.widgets[0].rect.topleft= vectorSum( xyOffset, (190,150) ) 
		self.widgets[1].rect.topleft= vectorSum( xyOffset, (20,20) )
                if len(self.widgets) == 3:
                        self.widgets[2].rect.topleft= vectorSum( xyOffset, (10,150) )

	#----------------------------------------------------------------------
 	def kill(self ):
		GUIView.kill( self )
		self.background.kill()
		del self.background


#------------------------------------------------------------------------------
class DadosDialogController(GUIController):
	"""..."""
	def __init__(self, evManager ):
		self.evManager = evManager
		self.evManager.RegisterListener( self )


	#----------------------------------------------------------------------
	def WantsEvent( self, event ):
		return 1

	#----------------------------------------------------------------------
	def HandlePyGameEvent(self, event):
		ev = None


		if event.type == KEYDOWN \
		     and (event.key == K_RETURN \
		     or event.key == K_SPACE):
			ev = GUIPressEvent()


		elif event.type == MOUSEMOTION:
			ev = GUIMouseMoveEvent(event.pos)

		elif event.type == MOUSEBUTTONUP:
			b = event.button
			if b == 1:
				ev = GUIClickEvent(event.pos)

		if ev:
			self.evManager.Post( ev )


#------------------------------------------------------------------------------
class DadosDialogView(GUIView):
	"""
        Essa classe precisa receber um vetor de resultados de dados e a string
        'ataque' para dados de ataque ou 'defesa' para dados de defesa
        """
	clipRect = pygame.Rect( (20,20,400,400) )

	#----------------------------------------------------------------------
	def __init__(self, evManager, renderGroup, rect, valores, acao):
		GUIView.__init__( self, evManager, renderGroup, rect)

		self.__msg = "Resultado dos dados de " + acao + ":"
                self.numDados = len(valores)
		bgSprite = pygame.sprite.Sprite()
		bgSprite.image = load_png( 'dialog_background.png' )
		bgSprite.rect = self.rect

		closeEvent = GUIDialogRemoveRequest( 'dadosDialog' )
		closeButton = ButtonSprite( evManager, "Fechar", 
		                            container=self, 
		                            onClickEvent=closeEvent, fontSize=25 )
		self.background = bgSprite
                
                self.widgets = [closeButton]
                for i in valores:
                        dado = 'data/' + str(i) + '.png'
                        self.widgets.append(ImageSprite(evManager, dado))

		self.widgets.append(LabelSprite(evManager, self.__msg, container=self, fontSize=25,\
                                                fontColor=(255,255,255)))

		#because this is a Dialog, it should be shown on the topmost
		#layer of the View.
		#the renderGroup here is expected to be a LayeredSpriteGroup
		self.renderGroup.add_top( self.background )
		for wid in self.widgets:
			self.renderGroup.add_top( wid )

		self.ArrangeWidgets()

	#----------------------------------------------------------------------
 	def ArrangeWidgets(self ):
		xyOffset = ( self.rect.x, self.rect.y )
		self.widgets[0].rect.topleft= vectorSum( xyOffset, (210,150) ) 
		self.widgets[-1].rect.topleft= vectorSum( xyOffset, (22,10) )
                for i in range (1, self.numDados+1):
                        x = 105*(i-1)
                        self.widgets[i].rect.topleft = vectorSum(xyOffset, (x, 35))

	#----------------------------------------------------------------------
 	def kill(self ):
		GUIView.kill( self )
		self.background.kill()
		del self.background


#------------------------------------------------------------------------------
class WaitingDialogView(GUIView):
	"""
        Classe para espera pelo inicio de jogo
        """
	clipRect = pygame.Rect( (20,20,400,400) )

	#----------------------------------------------------------------------
	def __init__(self, evManager, renderGroup, rect, game):
		GUIView.__init__( self, evManager, renderGroup, rect, game)

		self.__msg = "Aguardando os outros jogadores confirmarem..."
		bgSprite = pygame.sprite.Sprite()
		bgSprite.image = load_png( 'dialog_background.png' )
		bgSprite.rect = self.rect
                pronto = ClickableImageSprite(evManager,'data/pronto1.png','data/pronto2.png',container=self,event=ServerIAmReadyEvent())

		closeEvent = GUIDialogRemoveRequest( 'waitingDialog' )
                texto = LabelSprite(evManager, self.__msg, container=self, fontSize=18,\
                                                fontColor=(255,255,255))
		self.background = bgSprite
                
                self.widgets = [texto,pronto]

		#because this is a Dialog, it should be shown on the topmost
		#layer of the View.
		#the renderGroup here is expected to be a LayeredSpriteGroup
		self.renderGroup.add_top( self.background )
		for wid in self.widgets:
			self.renderGroup.add_top( wid )

		self.ArrangeWidgets()

	#----------------------------------------------------------------------
 	def ArrangeWidgets(self ):
		xyOffset = ( self.rect.x, self.rect.y )
		self.widgets[0].rect.topleft= vectorSum( xyOffset, (20,20) )
                self.widgets[1].rect.topleft= vectorSum( xyOffset, (80, 60) )

	#----------------------------------------------------------------------
 	def kill(self):
		GUIView.kill( self )
		self.background.kill()
		ev = GUIChangeScreenRequest( 'main' )
		self.evManager.Post(ev)
		del self.background


if __name__ == "__main__":
	print "that was unexpected"
